//
//  CYSearchTVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/23.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYSearchTVController.h"
#import "CYPerson.h"
#import "RHPerson.h"
#import "CYNavigationVController.h"
#import "CYABDetailVController.h"
#import "DSNavigationBar.h"
#import <Foundation/Foundation.h>

@interface CYSearchTVController ()

@end

@implementation CYSearchTVController

- (void)setPersons:(NSArray *)persons {
    _persons = persons;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//     self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.persons.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    CYPerson *person = self.persons[indexPath.row];
    cell.textLabel.text = person.person.name;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"共有%lu个搜索结果", (unsigned long)self.persons.count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CYPerson *person = self.persons[indexPath.row];
//    person.pushTag = 2;
//    CYABDetailVController *abController = [self.storyboard instantiateViewControllerWithIdentifier:@"CYABDetailVController"];
//    CYABDetailVController *controll = [[CYABDetailVController alloc] init];
//    controll.person = person;
//    controll.view.backgroundColor = [UIColor whiteColor];
//    CYNavigationVController *navi = [[CYNavigationVController alloc] initWithRootViewController:controll];
//    [self presentViewController:navi animated:YES completion:nil];
//    [self.navigationController pushViewController:controll animated:YES];
    
    //通知代理cell点击
    if ([self.delegate respondsToSelector:@selector(tableView:cellDidSelected:)]) {
        [self.delegate tableView:self cellDidSelected:[tableView cellForRowAtIndexPath:indexPath]];
    }
    
    CYABDetailVController *controll = [[CYABDetailVController alloc] init];
    controll.person = person;
    controll.hidesBottomBarWhenPushed = YES;
    
//    UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[DSNavigationBar class] toolbarClass:nil];
//    [navi setViewControllers:@[controll] animated:YES];
//    navi.navigationBar.barStyle = UIStatusBarStyleLightContent;
            person.pushTag = 1;
//    [self presentViewController:navi animated:YES completion:nil];
    [self.navigationController pushViewController:controll animated:YES];
}

- (void)dealloc {
    CYLog(@"通讯录搜索销毁");
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
